package org.freedoit.server.dao;

// Generated Oct 11, 2012 3:54:06 PM by Hibernate Tools 3.4.0.CR1

import java.sql.Timestamp;
import java.util.List;
import java.util.logging.Logger;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.TemporalType;
import javax.persistence.TypedQuery;

import org.freedoit.server.om.Task;
import org.freedoit.shared.exception.ServerException;


/**
 * Home object for domain model class Task.
 * @see org.freedoit.om.Task
 * @author Hibernate Tools
 */
public class TaskDAO {

	private static final Logger log = Logger.getLogger(TaskDAO.class.getName());

	private final EntityManagerFactory emFactory = DAOFactory.getEntityManagerFactory();

	public void persist(Task task) {
		log.config("persisting Task instance");
		EntityManager em = emFactory.createEntityManager();
		try {
			em.getTransaction().begin();
			em.persist(task);

			em.getTransaction().commit();
			log.info("persist successful");
		} catch (RuntimeException re) {
			log.severe("persist failed:" + re.getMessage());
			throw new ServerException(re);
		} finally {
			em.close();
		}
	}

	public void update(Task task) {
		//log.debug("updating Task instance");
		EntityManager em = emFactory.createEntityManager();
		try {
			em.getTransaction().begin();
			em.merge(task);
			em.getTransaction().commit();
			//log.debug("update successful");
		} catch (RuntimeException re) {
			//log.error("update failed", re);
			throw new ServerException(re);
		} finally {
			em.close();
		}
	}

	public void saveOrUpdate(Task task) {
		//log.debug("updating Task instance");
		update(task);
	}

	public Task findById(Long id) {
		//log.debug("getting Task instance with id: " + id);
		EntityManager em = emFactory.createEntityManager();
		try {
			Task instance = em.find(Task.class, id);
			if (instance == null) {
				//log.debug("get successful, no instance found");
			} else {
				//log.debug("get successful, instance found");
			}
			return instance;
		} catch (RuntimeException re) {
			//log.error("get failed", re);
			throw new ServerException(re);
		} finally {
			em.close();
		}
	}

	public List<Task> findByUserId(Long userId) {
		//log.debug("finding Task instances by user id:" + userId);
		EntityManager em = emFactory.createEntityManager();
		try {
			StringBuffer sbQuery = new StringBuffer();
			sbQuery.append("from ").append(Task.class.getName());
			sbQuery.append(" where userId = ").append(userId);
			List<Task> results = em.createQuery(sbQuery.toString(), Task.class).getResultList();
			//log.debug("find by user id successful, result size: " + results.size());
			return results;
		} catch (RuntimeException re) {
			//log.error("find by user id failed", re);
			throw new ServerException(re);
		} finally {
			em.close();
		}
	}

	public List<Task> findLatest(Long userId, Long clientId, Timestamp timestamp, 
			Timestamp now) {
		//log.debug("finding Task instances by user id and timestamp:" + userId + " " + timestamp);
		EntityManager em = emFactory.createEntityManager();
		try {
			StringBuffer sbQuery = new StringBuffer();
			sbQuery.append("from ").append(Task.class.getName());
			sbQuery.append(" where userId = ").append(userId);
			sbQuery.append(" and lastModifyByClientId <> ").append(clientId);
			
			if (timestamp != null) {
				sbQuery.append(" and lastModifyTime between ?1 and ?2 ");
			} else {
				sbQuery.append(" and lastModifyTime < ?2 ");
			}
			TypedQuery<Task> q = em.createQuery(sbQuery.toString(), Task.class);
			
			if (timestamp != null) {
				q.setParameter(1, timestamp, TemporalType.TIMESTAMP);
			}
			q.setParameter(2, now, TemporalType.TIMESTAMP);
			List<Task> results = q.getResultList();
			//log.debug("find by user id and timestamp successful, result size: " + results.size());
			return results;
		} catch (RuntimeException re) {
			//log.error("find by user id and timestamp failed", re);
			throw new ServerException(re);
		} finally {
			em.close();
		}
	}
}
